package com.jshongli.myapplication.inspect;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.jshongli.myapplication.R;
import com.jshongli.myapplication.base.BaseActivity;
import com.jshongli.myapplication.common.utils.CroodUtils;
import com.jshongli.myapplication.common.utils.NotificationUtil;
import com.jshongli.myapplication.common.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LocationActivity extends BaseActivity {

    @BindView(R.id.mapView)
    MapView mMapView;

    BaiduMap mBaiduMap;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_location;
    }

    @Override
    protected void initView() {
        super.initView();

        mToolbar.setTitle("当前定位");
        initToolbarBack(mToolbar);

        initTvRightText(mToolbar,"确认",null);

        mBaiduMap = mMapView.getMap();

        initPD();
        initMap();
        pd.show();
    }

    //region initPD
    private ProgressDialog pd;

    private void initPD() {
        pd = new ProgressDialog(mContext);
        pd.setCanceledOnTouchOutside(false);
        pd.setCancelable(false);
        pd.setMessage("正在定位中...");
    }
    //endregion

    //region 地图生命周期
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        if(mLocationClient != null)
        {
            mLocationClient.stop();
        }
        mBaiduMap.setMyLocationEnabled(false);
        mMapView = null;


    }
    //endregion

    //region 通过LocationClient发起定位
    public LocationClient mLocationClient = null;

    private void initMap() {

        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
                MyLocationConfiguration.LocationMode.FOLLOWING, true, null));




        mLocationClient = new LocationClient(this);

//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
//        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

//设置locationClientOption
        mLocationClient.setLocOption(option);

//注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();
        NotificationUtil.setNotification(mContext, "正在使用定位...");

    }
    //endregion

    LatLng lastPoints;
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }

            pd.dismiss();

            Log.d("weidu --- ", location.getLatitude() + "");
            Log.d("jingdu --- ", location.getLongitude() + "");

            lastPoints = new LatLng(location.getLatitude(), location.getLongitude());
            //显示当前定位点，缩放地图
            locateAndZoom(location, lastPoints);
        }
    }
    //endregion

    //region 中心位置
    private void locateAndZoom(BDLocation location, LatLng ll) {
        /**
         * 记录当前经纬度，当位置不变，手机转动，取得方向传感器的方向，
         给地图重新设置位置参数，在跟随模式下可使地图箭头随手机转动而转动
         */
//        mCurrentLat = location.getLatitude();
//        mCurrentLon = location.getLongitude();
        MyLocationData locData = new MyLocationData.Builder().accuracy(0)//去掉精度圈
                //此mCurrentDirection为自己获取到的手机传感器方向信息，顺时针0-360
                .direction(location.getDirection()).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        mBaiduMap.setMyLocationData(locData);//显示当前定位位置点

        //给地图设置缩放中心点，和缩放比例值
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(ll).zoom(18.0f);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }
    //endregion

    @Override
    protected void onTvRightClicked() {
        super.onTvRightClicked();

        Intent intent = new Intent();
        intent.putExtra("wd", lastPoints.latitude + "");
        intent.putExtra("jd", lastPoints.longitude + "");
        setResult(1892, intent);
        finish();
    }


}